<template>
  <div class="app-container info-fixed">
    <div class="main-content">
      <!-- 左侧内容区域 -->
      <div class="left-content">
        <!-- 搜索条件卡片 -->
        <el-card class="search-card" shadow="hover">
          <div slot="header" class="clearfix card-header-flex">
            <span><i class="el-icon-search card-header-icon"></i> 搜索条件</span>
            <div class="header-right-group">
              <el-button
                class="toggle-search-btn"
                type="text"
                size="mini"
                @click="showSearch = !showSearch"
                :icon="showSearch ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              >
                {{ showSearch ? '收起' : '展开' }}
              </el-button>
            </div>
          </div>
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="false"
            v-show="showSearch"
            label-width="80px"
            class="search-form-grid"
          >
            <el-row :gutter="20">
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="订单号" prop="orderIdView">
                  <el-input v-model="queryParams.orderIdView" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="游戏名" prop="gameName">
                  <el-select v-model="queryParams.gameName" placeholder="请选择游戏名" clearable filterable @change="handleQuery" style="width: 100%">
                    <el-option v-for="item in gameNameOptions" :key="item" :label="item" :value="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="游戏内容" prop="project">
                  <el-input v-model="queryParams.project" placeholder="请输入代练/陪玩内容" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="状态" prop="orderSta">
                  <el-select v-model="queryParams.orderSta" placeholder="请选择状态" clearable style="width: 100%">
                    <el-option v-for="dict in dict.type.order_sta" :key="dict.value" :label="dict.label" :value="dict.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="接手人" prop="recManName">
                  <el-input v-model="queryParams.recManName" placeholder="请输入接手人" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="结算状态" prop="isSettStatus">
                  <el-select v-model="queryParams.isSettStatus" placeholder="请选择结算状态" clearable style="width: 100%">
                    <el-option v-for="dict in dict.type.is_sett_status" :key="dict.value" :label="dict.label" :value="dict.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="发单人" prop="assUserName">
                  <el-input v-model="queryParams.assUserName" placeholder="请输入发单人" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="录单时间">
                  <el-date-picker
                    v-model="daterangeAssTime"
                    style="width: 100%"
                    value-format="yyyy-MM-dd"
                    type="daterange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="4">
                <el-form-item label="打手编号" prop="jiwei">
                  <el-input v-model="queryParams.jiwei" placeholder="请输入打手编号" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="24" :md="4" class="search-btn-col">
                <el-form-item>
                  <div class="search-btn-group">
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['order:info:export']">导出</el-button>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>

        <!-- 订单列表卡片 -->
        <el-card class="order-list-card" shadow="hover">
          <div slot="header" class="clearfix card-header-flex">
            <span><i class="el-icon-document card-header-icon"></i> 订单信息列表</span>
            <div class="header-right-group">
              <span class="header-right-info">共 {{ total }} 条记录</span>
            </div>
          </div>
          <div class="order-table-wrapper">
            <el-table
              v-loading="loading"
              :data="infoList"
              @selection-change="handleSelectionChange"
              stripe
              border
            >
              <el-table-column label="序号" type="index" width="50" align="center"  />
              <el-table-column label="订单号" align="center" prop="orderIdView" width="180">
                <template slot-scope="scope">
                  <el-tooltip class="item" effect="dark" :content="scope.row.orderIdView" placement="top" >
                    <div class="order-id-cell" @click="copyText(scope.row.orderIdView)">
                      {{ scope.row.orderIdView }}
                    </div>
                  </el-tooltip>
                </template>
              </el-table-column>
              <el-table-column label="游戏名" align="center" prop="gameName"  width="120"/>
              <el-table-column label="代练/陪玩内容" align="center" prop="project" >
                <template slot-scope="scope">
                  <el-tooltip class="item" effect="dark" :content="scope.row.project" placement="top">
                    <div class="project-cell">
                      {{ scope.row.project }}
                    </div>
                  </el-tooltip>
                </template>
              </el-table-column>
              <el-table-column label="状态" align="center" prop="orderSta" width="80">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.order_sta" :value="scope.row.orderSta" />
                </template>
              </el-table-column>
              <el-table-column label="接手人" align="center" prop="recManName" width="80" />
              <el-table-column label="发单价" align="center" prop="recFee" width="60" />
              <el-table-column label="接单价" align="center" prop="price" width="60" />
              <el-table-column label="结算状态" align="center" prop="isSettStatus" width="80">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.is_sett_status" :value="scope.row.isSettStatus" />
                </template>
              </el-table-column>
              <el-table-column label="发单人" align="center" prop="assUserName" width="90" />
              <el-table-column label="录单时间" align="center" prop="assTime" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.assTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="打手编号" align="center" prop="jiwei" width="80"/>
              <el-table-column label="重要备注" align="center" prop="orderRemark" width="100" />
            </el-table>
          </div>
          <div class="pagination-wrapper">
            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
              style="margin-top: 15px; margin-bottom: 10px;"
            />
          </div>
        </el-card>
      </div>
    </div>
    <!-- 弹窗表单区保持原样 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="系统内置单号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入系统内置单号" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderIdView">
          <el-input v-model="form.orderIdView" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="游戏名" prop="gameName">
          <el-input v-model="form.gameName" placeholder="请输入游戏名" />
        </el-form-item>
        <el-form-item label="代练/陪玩内容" prop="project">
          <el-input v-model="form.project" placeholder="请输入代练/陪玩内容" />
        </el-form-item>
        <el-form-item label="状态" prop="orderSta">
          <el-select v-model="form.orderSta" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.order_sta"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接手人" prop="recManName">
          <el-input v-model="form.recManName" placeholder="请输入接手人" />
        </el-form-item>
        <el-form-item label="发单价" prop="recFee">
          <el-input v-model="form.recFee" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="接单价" prop="price">
          <el-input v-model="form.price" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="结算状态" prop="isSettStatus">
          <el-select v-model="form.isSettStatus" placeholder="请选择结算状态">
            <el-option
              v-for="dict in dict.type.is_sett_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发单人" prop="assUserName">
          <el-input v-model="form.assUserName" placeholder="请输入发单人" />
        </el-form-item>
        <el-form-item label="录单时间" prop="assTime">
          <el-date-picker clearable
            v-model="form.assTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择录单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="机位/团长" prop="jiwei">
          <el-input v-model="form.jiwei" placeholder="请输入机位/团长" />
        </el-form-item>
        <el-form-item label="重要备注" prop="orderRemark">
          <el-input v-model="form.orderRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listInfo, getInfo, delInfo, addInfo, updateInfo, gameType} from "@/api/order/info"

export default {
  name: "Info",
  dicts: ['order_sta', 'is_sett_status'],
  data() {
    return {
      gameNameOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据汇总表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 重要备注时间范围
      daterangeAssTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderIdView: null,
        gameName: null,
        project: null,
        orderSta: null,
        recManName: null,
        recFee: null,
        price: null,
        isSettStatus: null,
        assUserName: null,
        assTime: null,
        jiwei: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "系统内置单号不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getGameType()
    this.getList()

  },
  methods: {
    getGameType() {
      gameType().then(response => {
        this.gameNameOptions = response;
      }).catch(error => {
        this.$message.error("获取游戏类型失败！");
        console.error(error);
      });
    },
    // 文本复制
    copyText(text) {
      navigator.clipboard.writeText(text).then(() => {
        this.$message.success('已复制订单号');
      }).catch(() => {
        this.$message.error('复制失败，请手动复制');
      });
    },
    /** 查询数据汇总列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeAssTime && '' != this.daterangeAssTime) {
        this.queryParams.params["beginAssTime"] = this.daterangeAssTime[0]
        this.queryParams.params["endAssTime"] = this.daterangeAssTime[1]
      }
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderIdView: null,
        gameName: null,
        project: null,
        orderSta: null,
        recManName: null,
        recFee: null,
        price: null,
        isSettStatus: null,
        assUserName: null,
        assTime: null,
        jiwei: null,
        orderRemark: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAssTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加数据汇总"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改数据汇总"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除数据汇总编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>
/* 统一表格每行高度 */
::v-deep .el-table__row {
  height: 40px;
}

/* 调整表格单元格内文字的垂直居中与行高匹配 */
::v-deep .el-table__cell {
  padding-top: 0;
  padding-bottom: 0;
  line-height: 40px; /* 之前是 40 px，修正空格 */
}

.app-container.info-fixed {
  min-height: calc(100vh - 84px);
  padding: 20px;
  background-color: #f5f7fa;
  box-sizing: border-box;
  overflow-y: auto;
}
.main-content {
  display: flex;
  align-items: stretch;
  height: 100%;
  gap: 20px;
}
.left-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-width: 0;
}
.search-card, .order-list-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}
.card-header-flex {
  display: flex;
  align-items: center;
  min-height: 36px;
  font-size: 16px;
  font-weight: 500;
}
.card-header-icon {
  font-size: 18px;
  margin-right: 6px;
  vertical-align: middle;
  display: inline-flex;
  align-items: center;
  line-height: 1;
}
.search-form-grid .el-form-item {
  margin-bottom: 12px;
}
.search-btn-group {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  align-items: center;
  height: 100%;
}
.search-btn-col {
  display: flex;
  align-items: flex-end;
}
.order-list-card {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.order-table-wrapper {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
}
.order-list-card .el-table {
  flex: 1;
  min-height: 0;
  height: 100%;
}
.pagination-wrapper {
  flex-shrink: 0;
}
.order-id-cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  color: #409eff;
  font-weight: 500;
}
.order-id-cell:hover {
  color: #66b1ff;
  text-decoration: underline;
}
.project-cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.header-right-info {
  color: #909399;
  font-size: 12px;
  margin-left: auto;
}
.toggle-search-btn {
  margin-left: 16px;
  font-size: 13px;
  transition: color 0.2s;
  vertical-align: middle;
}
.header-right-group {
  display: flex;
  align-items: center;
  margin-left: auto;
  gap: 12px;
}
@media (max-width: 1200px) {
  .main-content {
    flex-direction: column;
    height: auto;
  }
  .left-content {
    width: 100%;
    min-width: 0;
    height: auto;
  }
  .order-list-card {
    height: auto;
    min-height: 0;
  }
}
@media (max-width: 768px) {
  .app-container.info-fixed {
    padding: 10px;
  }
  .main-content {
    gap: 10px;
  }
  .search-btn-group {
    flex-direction: column;
    gap: 10px;
  }
  .toggle-search-btn {
    margin-left: 8px;
    min-width: 60px;
  }
}
</style>
